package net.xinqushi.design.holder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Holder 是单例模式的一种，Spring中的ApplicationContextHolder
 */
public class CacheHolder {

    private static Cache<String,Object> cache;

    public void setCache(Cache c) {
        if(cache != null) throw new IllegalStateException("CacheHolder already holded 'cache'");
        cache = c;
    }
    public static Cache getCache(){
        return cache;
    }
    //省略了其它N多cache静态方法
    public static void add(String key, Object value, String expiration) {
        cache = cache == null ? new Cache<String, Object>() : cache;
        cache.add(key, value, expiration);
    }
    public static void cleanHolder() {
        cache = null;
    }
    static class Cache<K,V>{
        final String defaultExpire = "1000";
        Map<K,V> cacheMap = new ConcurrentHashMap<K,V>();
        void add(K key , V value){
            add(key,value,null);
        }
        void add(K key , V value , String expire){
            if(null == expire) {// add cache has default expiration
            }else{//cache add key using specified expiration
                 }
        }

    }
}

